package com.hiultra.archive.service.impl;

import java.util.List;

import com.hiultra.archive.domain.ArcAppraialDetails;
import com.hiultra.archive.domain.ArcArchive;
import com.hiultra.archive.mapper.ArcArchiveMapper;
import com.hiultra.common.exception.ServiceException;
import com.hiultra.common.utils.ArcUtil;
import com.hiultra.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.hiultra.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.hiultra.archive.domain.ArcDestroyDetail;
import com.hiultra.archive.mapper.ArcDestroyMapper;
import com.hiultra.archive.domain.ArcDestroy;
import com.hiultra.archive.service.IArcDestroyService;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

/**
 * 销毁Service业务层处理
 * 
 * @author ls
 * @date 2023-09-02
 */
@Service
public class ArcDestroyServiceImpl implements IArcDestroyService 
{
    @Autowired
    private ArcDestroyMapper arcDestroyMapper;
    @Autowired
    private ArcArchiveMapper arcArchiveMapper;

    /**
     * 查询销毁
     * 
     * @param destroyId 销毁主键
     * @return 销毁
     */
    @Override
    public ArcDestroy selectArcDestroyByDestroyId(Long destroyId)
    {
        return arcDestroyMapper.selectArcDestroyByDestroyId(destroyId);
    }

    /**
     * 查询销毁列表
     * 
     * @param arcDestroy 销毁
     * @return 销毁
     */
    @Override
    public List<ArcDestroy> selectArcDestroyList(ArcDestroy arcDestroy)
    {
        return arcDestroyMapper.selectArcDestroyList(arcDestroy);
    }

    /**
     * 新增销毁
     * 
     * @param arcDestroy 销毁
     * @return 结果
     */
    @Transactional
    @Override
    public int insertArcDestroy(ArcDestroy arcDestroy)
    {
//        arcDestroy.setCreateTime(DateUtils.getNowDate());
//        int rows = arcDestroyMapper.insertArcDestroy(arcDestroy);
//        insertArcDestroyDetail(arcDestroy);
//        return rows;

        int rows = 0;
        try {
            arcDestroy.setCreateTime(DateUtils.getNowDate());
            // 数据判空
            if (StringUtils.isNull(arcDestroy.getArcDestroyDetailList())) {
                throw new ServiceException("销毁明细为空，请检查重试！");
            }
            //生成单号以及其他信息
            arcDestroy.setDestroyNo("XH" + ArcUtil.getOrderNo());
            arcDestroy.setDestroyTime(DateUtils.getNowDate());
            //循环处理子表明细同时处理各种状态
            rows = arcDestroyMapper.insertArcDestroy(arcDestroy);
            for (ArcDestroyDetail arcDestroyDetail : arcDestroy.getArcDestroyDetailList()) {
                //修改档案信息
                ArcArchive archive = new ArcArchive();
                archive.setArchiveId(arcDestroyDetail.getArchiveId());
                archive.setAppraisal(4);
                arcArchiveMapper.updateArcArchive(archive);
            }

            insertArcDestroyDetail(arcDestroy);
            // 提交事务
            return rows;
        } catch (Exception e) {
            // 手动回滚事务
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            throw e;
        }
    }

    /**
     * 修改销毁
     * 
     * @param arcDestroy 销毁
     * @return 结果
     */
    @Transactional
    @Override
    public int updateArcDestroy(ArcDestroy arcDestroy)
    {
//        arcDestroy.setUpdateTime(DateUtils.getNowDate());
        arcDestroyMapper.deleteArcDestroyDetailByDestroyId(arcDestroy.getDestroyId());
//        insertArcDestroyDetail(arcDestroy);
//        return arcDestroyMapper.updateArcDestroy(arcDestroy);

        int rows = 0;
        try {
            arcDestroy.setCreateTime(DateUtils.getNowDate());
            // 数据判空
            if (StringUtils.isNull(arcDestroy.getArcDestroyDetailList())) {
                throw new ServiceException("销毁明细为空，请检查重试！");
            }
            //生成单号以及其他信息
            arcDestroy.setDestroyNo("XH" + ArcUtil.getOrderNo());
            arcDestroy.setDestroyTime(DateUtils.getNowDate());
            //循环处理子表明细同时处理各种状态
            rows = arcDestroyMapper.insertArcDestroy(arcDestroy);
            for (ArcDestroyDetail arcDestroyDetail : arcDestroy.getArcDestroyDetailList()) {
                //修改档案信息
                ArcArchive archive = new ArcArchive();
                archive.setArchiveId(arcDestroyDetail.getArchiveId());
                archive.setAppraisal(4);
                arcArchiveMapper.updateArcArchive(archive);
            }

            insertArcDestroyDetail(arcDestroy);
            // 提交事务
            return rows;
        } catch (Exception e) {
            // 手动回滚事务
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            throw e;
        }
    }

    /**
     * 批量删除销毁
     * 
     * @param destroyIds 需要删除的销毁主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteArcDestroyByDestroyIds(Long[] destroyIds)
    {
        arcDestroyMapper.deleteArcDestroyDetailByDestroyIds(destroyIds);
        return arcDestroyMapper.deleteArcDestroyByDestroyIds(destroyIds);
    }

    /**
     * 删除销毁信息
     * 
     * @param destroyId 销毁主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteArcDestroyByDestroyId(Long destroyId)
    {
        arcDestroyMapper.deleteArcDestroyDetailByDestroyId(destroyId);
        return arcDestroyMapper.deleteArcDestroyByDestroyId(destroyId);
    }

    /**
     * 新增销毁详情信息
     * 
     * @param arcDestroy 销毁对象
     */
    public void insertArcDestroyDetail(ArcDestroy arcDestroy)
    {
        List<ArcDestroyDetail> arcDestroyDetailList = arcDestroy.getArcDestroyDetailList();
        Long destroyId = arcDestroy.getDestroyId();
        if (StringUtils.isNotNull(arcDestroyDetailList))
        {
            List<ArcDestroyDetail> list = new ArrayList<ArcDestroyDetail>();
            for (ArcDestroyDetail arcDestroyDetail : arcDestroyDetailList)
            {
                arcDestroyDetail.setDestroyId(destroyId);
                list.add(arcDestroyDetail);
            }
            if (list.size() > 0)
            {
                arcDestroyMapper.batchArcDestroyDetail(list);
            }
        }
    }
}
